package com.ddwl.user.dao;

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.schema.bo.user.LoginLogPageBo;
import com.ddwl.user.constant.LoginLogTypeEnum;
import com.ddwl.user.dao.mapper.LoginLogMapper;
import com.ddwl.user.dao.model.LoginLog;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2020/9/16 13:54
 */
@Repository
public class LoginLogDao{

    @Resource
    private LoginLogMapper loginLogMapper;

    public int insert(LoginLog loginLog) {
        if(StrUtil.isEmpty(loginLog.getId())){
            loginLog.setId(UUIDGenerator.getShortUUID());
        }
        return loginLogMapper.insert(loginLog);
    }

    public Integer getCountByUserId(String userId) {
        return loginLogMapper.getCountByUserId(userId);
    }

    public LoginLog getFirstLogin(LoginLog loginLog) {
        return loginLogMapper.getgetFirstLogin(loginLog);
    }

    public List<LoginLog> getList(LoginLogPageBo pageBo) {
        Example example = new Example(LoginLog.class);
        example.and().andEqualTo("pk", pageBo.getPk())
                .andEqualTo("type", LoginLogTypeEnum.LOGIN.getValue());
        return loginLogMapper.selectByExample(example);
    }
}
